import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Answer } from './answer.entity';
import { AnswerService } from './answer.service';
import request from 'request'

import {
  apibefore
} from '../../router'
import { UserService } from '../user/user.service';
import utils from 'src/common/utils';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
import { AnswerServiceManager } from './answer.service.manager';
import { User } from '../user/user.entity';
@Controller(apibefore('/answer_manager'))
export class AnswerControllerManager {
  constructor(private readonly service: AnswerServiceManager,
    private readonly answerService: AnswerService,
    private readonly userService: UserService) {}


  @Get('/findAnswerCheckNotNullByVideo')
  async findAnswerCheckNotNullByVideo(@Query() params, @Request() req){
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser: User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return this.service.findAnswerCheckNotNullByVideo(skip, take, params.video_id)
  }

  @Post('/changeAnswerIcGroup')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async changeAnswerIcGroup(@Body() params: { answer_id: number; ic_group: number }, @Request() req){
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser: User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let err = await this.service.changeAnswerIcGroup(params.answer_id, params.ic_group)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }

  @Post('/clearAnswerIcGroup')
  async clearAnswerIcGroup(@Body() params: { answer_id: number }, @Request() req){
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser: User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let err = await this.service.clearAnswerIcGroup(params.answer_id)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }


  @Post('/createAnswerIc')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async createAnswerIc(@Body() params: { answer_id: number; ic_group: number }, @Request() req) {
    let err = await this.answerService.createAnswerIc(params.answer_id)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return true
  }

}
